import msgCheck from '../../util/msgCheck'
import imgCheck from '../../util/imgCheck'
import uploadImgList from '../../util/uploadImgList'
import pointGoodsApi from '../../api/pointGoodsApi'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _id: '',
    title: '',
    imgList: [],
    fileList: [],
    deleteFiles: [],
    point: '',
    count: '',
    enumList: []
  },

  onLoad(options) {
    this.getDetail(options._id)
  },

  async getDetail(_id){
    if(!_id){
      return
    }
    let res = await pointGoodsApi.get(_id)
    this.setData({
      _id: res._id,
      title: res.title,
      point: res.point,
      count: res.count,
      imgList: res.imgList,
      fileList: res.fileList,
      enumList: res.enumList
    })
  },

  titleInput: function (e) {
    var val = e.detail.value;

    this.setData({
      title: val
    });
  },

  pointChange(e){
    console.log(e.detail.value)
    this.setData({
      point: e.detail.value
    })
  },
  pointCount(e){
    this.setData({
      count: e.detail.value
    })
  },

  previewImage(e) {
    var url = e.currentTarget.dataset.url
    wx.previewImage({
      urls: this.data.imgList,
      current: url
    })
  },

  chooseImage: function () {
    wx.chooseMedia({
      count: 9 - this.data.imgList.length,
      mediaType: ['image'],
      success: (res) => {
        let list = res.tempFiles.map(obj => obj.tempFilePath)

        const tempFilePaths = this.data.imgList.concat(list)
        this.setData({
          imgList: tempFilePaths
        })
      }
    })
  },

  delImg(e) {
    var index = e.currentTarget.dataset.index
    this.data.imgList.splice(index, 1)
    var fileIds = this.data.fileList.splice(index, 1)
    if (fileIds[0]) {
      this.data.deleteFiles.push(fileIds[0])
    }
    this.setData({
      imgList: this.data.imgList
    })
  },

  dictChange(e){
    this.setData({
      enumList: e.detail
    })
  },


  onChooseAvatar(e) {
    if (wx.getStorageSync('nickname')) {
      this.addGoods()
      return
    }
    this.selectComponent('#avatarNickname').open()

  },
  avatarNicknameConfirm(e) {

    this.addGoods()
  },

  async addGoods() {
    if (!this.data.title && this.data.imgList.length == 0) {
      wx.showToast({
        title: '没有可提交的内容',
        icon: 'none'
      })
      return
    }

    let pass = await msgCheck(this.data.title)
    if (!pass) {
      return
    }

    pass = await imgCheck(this.data.imgList)
    if (!pass) {
      return
    }

    wx.showLoading({
      title: '正在提交',
      mask: true
    })
    var res = await uploadImgList(this.data.imgList, this.data.fileList)

    let params = {
      point: parseInt(this.data.point) ,
      count: parseInt(this.data.count) ,
      title: this.data.title,
      fileList: res.map((f) => f.fileID),
      imgList: res.map((f) => f.tempFileURL),
      enumList: this.data.enumList
    }
    if(this.data._id){
      params._id = this.data._id
      await pointGoodsApi.update(params)
    } else {
      await pointGoodsApi.add(params)
    }

    wx.showToast({
      title: '提交成功',
    })
   
    wx.navigateBack()
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})